package com.oa.framework;

import com.oa.framework.acl.url.service.SystemUrlResourceService;
import com.oa.framework.org.department.service.DepartmentService;
import com.oa.framework.org.service.DutyService;
import com.oa.framework.org.service.PositionService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;


/**
 * OA框架引擎
 * 可以在脱离Spring 容器中使用
 * @author cuichao
 */
@Component
public final class FrameworkEngine implements BeanFactoryAware, InitializingBean, DisposableBean {

    private static BeanFactory beanFactory;
    /**
     * 职务服务
     */
    private static DutyService dutyService;
    /**
     * 职位服务
     */
    private static PositionService positionService;
    /**
     * 部门服务
     */
    private static DepartmentService departmentService;
    /**
     * 系统资源服务
     */
    private static SystemUrlResourceService resourceService;

    @Override
    public void afterPropertiesSet() throws Exception {
        dutyService = beanFactory.getBean(DutyService.class);
        positionService = beanFactory.getBean(PositionService.class);
        departmentService = beanFactory.getBean(DepartmentService.class);
        resourceService = beanFactory.getBean(SystemUrlResourceService.class);
    }



    public static DutyService getDutyService(){
        assertFrameworkEngine();
        return dutyService;
    }

    public static PositionService getPositionService(){
        assertFrameworkEngine();
        return positionService;
    }

    public static DepartmentService getDepartmentService(){
        assertFrameworkEngine();
        return departmentService;
    }

    public static SystemUrlResourceService getResourceService(){
        assertFrameworkEngine();
        return resourceService;
    }


    private static void assertFrameworkEngine(){
        Assert.notNull(beanFactory, FrameworkEngine.class.getSimpleName()+"is not be started");
    }

    @Override
    public void destroy() throws Exception {
        //no to do
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        FrameworkEngine.beanFactory = beanFactory;
    }



}
